package com.polaris.live.common.util

import com.blankj.utilcode.util.GsonUtils
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.resp.UserAccountExtResp
import com.polaris.live.common.resp.UserGameAccountExtBean
import com.polaris.live.common.resp.UserLevelAccountResp

/**
 * UserUtils
 *
 * @author Created by lucas on 2023/10/23 15:11
 */
object UserAccountUtils {

    var onAccountChangeListener: OnAccountChangeListener? = null
    var onGameAccountChangeListener: OnGameAccountChangeListener? = null
    var onLevelAccountChangeListener: OnLevelAccountChangeListener? = null
    private var lastTimes: Long? = null

    /**
     * 处理拓展的账户信息
     */
    fun handleExtAccount(extElement: JsonElement?, timestamp: Long? = null) {
        if (extElement is JsonObject) {
            val accountElement = extElement.get("account")
            handleAccount(accountElement, timestamp)
            val gameAccountElement = extElement.get("gameAccount")
            handleGameAccount(gameAccountElement, timestamp)
        }
    }

    /**
     * 处理拓展的账户信息
     */
    fun handleAccount(accountElement: JsonElement?, timestamp: Long? = null) {
        if (accountElement is JsonObject) {
            try {
                val resp =
                    GsonUtils.getGson().fromJson(accountElement, UserAccountExtResp::class.java)
                val account = UserManager.getUser().account
                val newResp = if (account != null) {
                    //是否继续分发    如果上次时间戳和这次的时间戳都不是空的 并且 上次的比这次的时间戳大，那么就不需要处理了
                    val currentLastTimes = lastTimes
                    val notOutGiving = if (currentLastTimes != null && timestamp != null) {
                        currentLastTimes > timestamp
                    } else false

                    if (notOutGiving) return

                    if (resp.coin >= 0) {
                        account.coins = resp.coin
                    }
                    if (resp.gainCoin >= 0) {
                        account.gainCoins = resp.gainCoin
                    }
                    if (resp.frozenCoin >= 0) {
                        account.frozenCoins = resp.frozenCoin
                    }
                    if (timestamp != null) {
                        lastTimes = timestamp
                    }

                    UserAccountExtResp(
                        account.coins,
                        account.gainCoins,
                        account.frozenCoins
                    )
                } else {
                    resp
                }

                onAccountChangeListener?.onAccountChange(newResp)
            } catch (_: Throwable) {
            }
        }
    }

    /**
     * 处理拓展的账户信息
     */
    fun handleGameAccount(accountElement: JsonElement?, timestamp: Long? = null) {
        if (accountElement is JsonObject) {
            try {
                val resp =
                    GsonUtils.getGson().fromJson(accountElement, UserGameAccountExtBean::class.java)
                val account = UserManager.getUser().account
                val newResp = if (account != null) {
                    //是否继续分发    如果上次时间戳和这次的时间戳都不是空的 并且 上次的比这次的时间戳大，那么就不需要处理了
                    val currentLastTimes = lastTimes
                    val notOutGiving = if (currentLastTimes != null && timestamp != null) {
                        currentLastTimes > timestamp
                    } else false

                    if (notOutGiving) return

                    if (resp.gameCoin >= 0) {
                        account.gameCoins = resp.gameCoin
                    }
                    if (resp.frozenGameCoin >= 0) {
                        account.frozenGameCoins = resp.frozenGameCoin
                    }
                    if (timestamp != null) {
                        lastTimes = timestamp
                    }

                    UserGameAccountExtBean(
                        account.gameCoins,
                        account.frozenGameCoins,
                    )
                } else {
                    resp
                }

                onGameAccountChangeListener?.onAccountChange(newResp)
            } catch (_: Throwable) {
            }
        }
    }


    /**
     * 处理豪魅值变化
     */
    fun handleLevelAccount(accountElement: JsonElement?, timestamp: Long? = null) {
        if (accountElement is JsonObject) {
            try {
                val resp =
                    GsonUtils.getGson().fromJson(accountElement, UserLevelAccountResp::class.java)
                val account = UserManager.getUser().account
                val newResp = if (account != null) {
                    //是否继续分发    如果上次时间戳和这次的时间戳都不是空的 并且 上次的比这次的时间戳大，那么就不需要处理了
                    val currentLastTimes = lastTimes
                    val notOutGiving = if (currentLastTimes != null && timestamp != null) {
                        currentLastTimes > timestamp
                    } else false

                    if (notOutGiving) return

                    if (resp.richs >= 0) {
                        account.richs = resp.richs
                    }
                    if (resp.charms >= 0) {
                        account.charms = resp.charms
                    }
                    if (timestamp != null) {
                        lastTimes = timestamp
                    }

                    UserLevelAccountResp(
                        account.richs,
                        account.charms,
                    )
                } else {
                    resp
                }

                onLevelAccountChangeListener?.onAccountChange(newResp)
            } catch (_: Throwable) {
            }
        }
    }
}

fun interface OnAccountChangeListener {
    fun onAccountChange(account: UserAccountExtResp)
}

fun interface OnGameAccountChangeListener {
    fun onAccountChange(account: UserGameAccountExtBean)
}

fun interface OnLevelAccountChangeListener {
    fun onAccountChange(account: UserLevelAccountResp)
}